/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitee.minimalismstyle.fresh.common.web.exception

import com.gitee.minimalismstyle.fresh.common.core.domain.R
import com.gitee.minimalismstyle.fresh.common.core.exception.MessageException
import com.gitee.minimalismstyle.fresh.common.core.extra.log
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody

/**
 *  统一异常web处理
 * @author maoxiaodong
 */
@ControllerAdvice
class GlobalExceptionHandler {

    /**
     * 不清楚的异常
     */
    @ExceptionHandler
    @ResponseBody
    fun handleException(e: Exception): R<*> {
        log.error(e.message, e)
        return R<Any>(success = false, code = 500, message = e.message)
    }

    /**
     * 消息异常
     */
    @ExceptionHandler(MessageException::class)
    @ResponseBody
    fun handleMessageException(e: MessageException): R<*> {
        log.error(e.message, e)
        return R(success = false, code = e.code, message = e.message, data = e.data)
    }
}
